#include "include/fncts.h"
#include "include/structs.h"
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <unistd.h>
char *getpass(const char *prompt);
char *get_current_dir_name(void);
void flush_input ( void )
{
    int ch;
    /* Lire tout les char execessive */ 
    do 
    ch = getchar(); 
    while ( ch != EOF && ch != '\n' ); 
    /* mettre EOF a zero */ 
    clearerr ( stdin ); 
}
void saisie_Med(staffmed *med)
{	
	int t;
	char buff[255];
	/*INPUT*/
	getchar();
        printf("[MED] Nom:");
        gets(med->nom);
        printf("[MED] Prenom:");
        gets(med->prenom);
        printf("[MED] Role:");
        gets(med->role);

        printf("[MED] ID:");
        fgets(buff,6,stdin);
        t=sscanf(buff,"%d",&(med->id));
        while(t!=1 || strlen(buff) != 5)
        {
		flush_input();
                printf("Incorrect Input ! \n");
                printf("[MED] ID:");
                fgets(buff,6,stdin);
                t=sscanf(buff,"%d",&(med->id));
        }

	flush_input();
	printf("[MED] CIN:");
	fgets(buff,9,stdin);
	t=sscanf(buff,"%ld",&(med->cin));
        while(t!=1 || strlen(buff) != 8)
	{
		flush_input();
		printf("Incorrect Input ! \n");
		printf("[MED] CIN:");
        	fgets(buff,9,stdin);
        	t=sscanf(buff,"%ld",&(med->cin));
	}
	flush_input();
	printf("[MED] Mot de passe:");
        gets(med->mdp);

        /*END OF INPUT*/
}
void saisie_Pat(patient *pat)
{	
	int t;
	char buff[255];
	/*INPUT*/
        getchar();
        printf("[PAT] Nom:");
        gets(pat->nom);
        printf("[PAT] Prenom:");
        gets(pat->prenom);
        printf("[PAT] Nom Maladie:");
        gets(pat->Nom_maladie);

	printf("[PAT] Date d'admission:");
        gets(pat->date_admission);

        printf("[PAT] ID:");
        fgets(buff,6,stdin);
        t=sscanf(buff,"%d",&(pat->id));
        while(t!=1 || strlen(buff)!= 5)
        {
                flush_input();
                printf("Incorrect Input ! \n");
                printf("[PAT] ID:");
                fgets(buff,6,stdin);
                t=sscanf(buff,"%d",&(pat->id));
        }

        flush_input();
	printf("[PAT] CIN:");
        fgets(buff,9,stdin);
        t=sscanf(buff,"%ld",&(pat->cin));
        while(t!=1 || strlen(buff) != 8)
        {
                flush_input();
                printf("Incorrect Input ! \n");
                printf("[PAT] CIN:");
                fgets(buff,9,stdin);
                t=sscanf(buff,"%ld",&(pat->cin));
        }
	flush_input();
        printf("[PAT] Age:");
        fgets(buff,3,stdin);
        t=sscanf(buff,"%d",&(pat->age));
        while(t!=1)
        {
                flush_input();
                printf("Incorrect Input ! \n");
                printf("[PAT] Age:");
                fgets(buff,3,stdin);
                t=sscanf(buff,"%d",&(pat->age));
        }
        /*END OF INPUT*/

}
void add_med(staffmed med,char subpath[])
{	
	char dirpath[1024];
	staffmed med1;
	/* OUTPUTS FILE TO <SUBPATH> IN CURRENT DIR */
	sprintf(dirpath,"%s",get_current_dir_name());
	strcat(dirpath,subpath);
	/*WRITING TO FILE*/
	if(read_med_file(subpath,med.id,&med1) == 1){printf("ID : %d existe deja ! \n",med.id);return;}
	FILE *dbmed = fopen(dirpath,"ab+");
	if(dbmed == NULL){printf("Error opening file !");return;}
	fwrite(&med,sizeof(staffmed),1,dbmed);
	/*END OF WRITING*/
	fclose(dbmed);//CLOSE FILE
}
void add_patient(patient pat,char subpath[])
{
	char dirpath[1024];
	patient pat1;
        /* OUTPUTS FILE TO <SUBPATH> IN CURRENT DIR */
        sprintf(dirpath,"%s",get_current_dir_name());
        strcat(dirpath,subpath);
        if(read_pat_file(subpath,pat.id,&pat1) == 1){printf("ID : %d existe deja ! \n",pat.id);return;}
	/*WRITING TO FILE*/
        FILE *dbpat = fopen(dirpath,"ab+");
	if(dbpat == NULL){printf("Error opening file !");return;}
	fwrite(&pat,sizeof(patient),1,dbpat);
        /*END OF WRITING*/
        fclose(dbpat);//CLOSE FILE
}
int read_med_file(char subpath[],int id,staffmed *med)
{
	char dirpath[1024];
        /* OUTPUTS FILE TO <SUBPATH> IN CURRENT DIR */
        sprintf(dirpath,"%s",get_current_dir_name());
        strcat(dirpath,subpath);
	FILE *dbmed = fopen(dirpath,"rb");
	if(dbmed==NULL){printf("Error opening file !");return -1;}
	rewind(dbmed);
	while(feof(dbmed)!=1 && id!=(*med).id)
        {
                fread(med,sizeof(staffmed),1,dbmed);
        }
        fclose(dbmed);
        if(id==(*med).id){return 1;}
        else return 0;

}
int read_pat_file(char subpath[],int id,patient *pat)
{
        char dirpath[1024];
	/* OUTPUTS FILE TO <SUBPATH> IN CURRENT DIR */
        sprintf(dirpath,"%s",get_current_dir_name());
        strcat(dirpath,subpath);
	FILE *dbpat=fopen(dirpath,"rb");
        if(dbpat==NULL){printf("Error opening file !");return -1;}
        rewind(dbpat);
        while(feof(dbpat)!=1 && id!=(*pat).id)
        {
                fread(pat,sizeof(patient),1,dbpat);
        }
        fclose(dbpat);
        if(id==(*pat).id){return 1;}
        else return 0;

}
void ListMed(char subpath[])
{
	staffmed med;
	char dirpath[1024];
        /* OUTPUTS FILE TO <SUBPATH> IN CURRENT DIR */
        sprintf(dirpath,"%s",get_current_dir_name());
        strcat(dirpath,subpath);
	FILE *file=fopen(dirpath,"rb");
        if(file==NULL){printf("Error Opening File for read\n");return;}
	printf("-----LISTE-STAFF-MED-------- \n");
        while(fread(&med,sizeof(staffmed),1,file)!=0)
        {
                printf("ID: %d | Nom: %s | Prenom: %s | Role : %s | CIN: %ld \n",med.id,med.nom,med.prenom,med.role,med.cin);
        }
	printf("-----FIN-LISTE-STAFF-MED---- \n");
        fclose(file);
}
void ListPat(char subpath[])
{
	char dirpath[1024];
	patient pat;
        /* OUTPUTS FILE TO <SUBPATH> IN CURRENT DIR */
        sprintf(dirpath,"%s",get_current_dir_name());
        strcat(dirpath,subpath);
	FILE *file=fopen(dirpath,"rb");
        if(file==NULL){printf("Error Opening File for read\n");return;}
	printf("----LISE-PATIENTS--------- \n");
        while(fread(&pat,sizeof(patient),1,file)!=0)
        {
                printf("ID: %d | Date admission : %s |Nom: %s | Prenom: %s | Maladie: %s | CIN: %ld \n",pat.id,pat.date_admission,pat.nom,pat.prenom,pat.Nom_maladie,pat.cin);
        }
	printf("----FIN-LSITE-PATIENTS---- \n");
        fclose(file);
}
void supprimer_med(char subpath[],int id)
{
	staffmed med;
	int isDeleted =0;
        char dirpath[1024],tmppath[1024];
        /* OUTPUTS FILE TO <SUBPATH> IN CURRENT DIR */
        sprintf(dirpath,"%s",get_current_dir_name());
	strcpy(tmppath,dirpath);
        strcat(dirpath,subpath);
	strcat(tmppath,"/tmp/tmpfilemed");
        FILE *dbmed=fopen(dirpath,"rb");
	FILE *tmpfile=fopen(tmppath,"wb");
        if(dbmed==NULL){printf("Error Opening %s for read\n",dirpath);return;}
	if(tmpfile==NULL){printf("Error Opening Tmpfile for read\n");return;}
	while(fread(&med,sizeof(staffmed),1,dbmed)!=0)
	{
		if(med.id!=id)
		{
		        fwrite(&med,sizeof(staffmed),1,tmpfile);

		}
		if(med.id == id)
		{
			printf("%d trouver et supprimer !",id);isDeleted = 1;
		}
	}
	if(isDeleted == 0){printf("%d introuvable !",id);}
	fclose(dbmed);
	fclose(tmpfile);
	remove(dirpath);
	rename(tmppath,dirpath);
}
void supprimer_pat(char subpath[],int id)
{	
	patient pat;
        int isDeleted =0;
        char dirpath[1024],tmppath[1024];
        /* OUTPUTS FILE TO <SUBPATH> IN CURRENT DIR */
        sprintf(dirpath,"%s",get_current_dir_name());
        strcpy(tmppath,dirpath);
        strcat(dirpath,subpath);
        strcat(tmppath,"/tmp/tmpfilepat");
        FILE *dbpat=fopen(dirpath,"rb");
        FILE *tmpfile=fopen(tmppath,"wb");
        if(dbpat==NULL){printf("Error Opening %s for read\n",dirpath);return;}
        if(tmpfile==NULL){printf("Error Opening Tmpfile for read\n");return;}
	while(fread(&pat,sizeof(patient),1,dbpat)!=0)
        {
                if(pat.id!=id)
                {
                        fwrite(&pat,sizeof(patient),1,tmpfile);

                }
                if(pat.id == id)
                {
                        printf("%d trouver et supprimer !",id);isDeleted = 1;
                }
        }
        if(isDeleted == 0){printf("%d introuvable !",id);}
        fclose(dbpat);
        fclose(tmpfile);
        remove(dirpath);
        rename(tmppath,dirpath);
}
void modifier_med(char subpath[])
{
	staffmed med,med1;
        int t = 0,id;
        char dirpath[1024],tmppath[1024],choix[10];
        /* OUTPUTS FILE TO <SUBPATH> IN CURRENT DIR */
        sprintf(dirpath,"%s",get_current_dir_name());
        strcpy(tmppath,dirpath);
        strcat(dirpath,subpath);
        strcat(tmppath,"/tmp/tmpfilemed");
	printf("ID de la personne a modifier : ");
        scanf("%d",&id);
        t=read_med_file(subpath,id,&med1);
	if( (t==0) || (t==-1) ){printf("%d introuvable",id);return;}
        printf("ID: %d | Nom: %s | Prenom: %s | Role : %s | CIN: %ld \n",med1.id,med1.nom,med1.prenom,med1.role,med1.cin); 
        printf("[MED] Entree le nom du champ a modifier : ");
        scanf("%s",choix);
	switch(toupper(choix[0]))
	{
		case 'I':{printf("[MED] ID : ");
        			scanf("%d",&(med1.id));break;}
		case 'N':{printf("[MED] Nom : ");
        			scanf("%s",med1.nom);break;}
		case 'P':{printf("[MED] Prenom : ");
        			scanf("%s",med1.prenom);break;}
		case 'R':{printf("[MED] Role : ");
        			scanf("%s",med1.role);break;}
		case 'C':{printf("[MED] CIN : ");
        			scanf("%ld",&(med1.cin));break;}
		default:{printf("Mauvais Choix : ");}
	}
        FILE *dbmed=fopen(dirpath,"rb");
        FILE *tmpfile=fopen(tmppath,"wb");
        if(dbmed==NULL){printf("Error Opening %s for read\n",dirpath);return;}
        if(tmpfile==NULL){printf("Error Opening Tmpfile for read\n");return;}
	rewind(dbmed);
	while(fread(&med,sizeof(staffmed),1,dbmed)!=0)
        {
                if(med.id!=id)
                {
                        fwrite(&med,sizeof(staffmed),1,tmpfile);

                }
                if(med.id == id)
                {
                        fwrite(&med1,sizeof(staffmed),1,tmpfile);
                }
        }
	fclose(dbmed);
        fclose(tmpfile);
        remove(dirpath);
        rename(tmppath,dirpath);
}
void modifier_pat(char subpath[])
{
        patient pat,pat1;
        int t = 0,id;
        char dirpath[1024],tmppath[1024],choix[20];
        /* OUTPUTS FILE TO <SUBPATH> IN CURRENT DIR */
        sprintf(dirpath,"%s",get_current_dir_name());
        strcpy(tmppath,dirpath);
        strcat(dirpath,subpath);
        strcat(tmppath,"/tmp/tmpfilepat");
        printf("ID de la personne a modifier : ");
        scanf("%d",&id);
        t=read_pat_file(subpath,id,&pat1);
        if( (t==0) || (t==-1) ){printf("%d introuvable",id);return;}
        printf("ID: %d | Date d'admission : %s |Nom: %s | Prenom: %s | Maladie: %s | CIN: %ld \n",pat1.id,pat1.date_admission,pat1.nom,pat1.prenom,pat1.Nom_maladie,pat1.cin); 
        printf("[PAT] Entree le nom du champ a modifier : ");
        scanf("%s",choix);
        switch(toupper(choix[0]))
        {
                case 'I':{printf("[PAT] ID : ");
                                scanf("%d",&(pat1.id));break;}
                case 'N':{printf("[PAT] Nom : ");
                                scanf("%s",pat1.nom);break;}
                case 'P':{printf("[PAT] Prenom : ");
                                scanf("%s",pat1.prenom);break;}
                case 'M':{printf("[PAT] Maladie : ");
                                scanf("%s",pat1.Nom_maladie);break;}
                case 'C':{printf("[PAT] CIN : ");
                                scanf("%ld",&(pat1.cin));break;}
		case 'D':{printf("[PAT] Date admission : ");
                                scanf("%s",pat1.date_admission);break;}
                default:{printf("Mauvais Choix : ");}
        }

        FILE *dbpat=fopen(dirpath,"rb");
        FILE *tmpfile=fopen(tmppath,"wb");
        if(dbpat==NULL){printf("Error Opening %s for read\n",dirpath);return;}
        if(tmpfile==NULL){printf("Error Opening Tmpfile for read\n");return;}
        rewind(dbpat);
	while(fread(&pat,sizeof(patient),1,dbpat)!=0)
        {
                if(pat.id!=id)
                {
                        fwrite(&pat,sizeof(patient),1,tmpfile);

                }
                if(pat.id == id)
                {
                        fwrite(&pat1,sizeof(patient),1,tmpfile);
                }
        }
        fclose(dbpat);
        fclose(tmpfile);
        remove(dirpath);
        rename(tmppath,dirpath);
}
int medtoid(char subpath[],char nom[])
{
	char dirpath[1024];
	staffmed med;
        /* OUTPUTS FILE TO <SUBPATH> IN CURRENT DIR */
        sprintf(dirpath,"%s",get_current_dir_name());
        strcat(dirpath,subpath);
        FILE *dbmed = fopen(dirpath,"rb");
        if(dbmed==NULL){printf("Error opening file !");return -1;}
        rewind(dbmed);
        while(feof(dbmed)!=1)
        {
		fread(&med,sizeof(staffmed),1,dbmed);
                if(strcmp(med.nom,nom)==0)
		{fclose(dbmed);return (med.id) ;}
        }
        fclose(dbmed);
        return -1;
}
int pattoid(char subpath[],char nom[])
{
        char dirpath[1024];
        patient pat;
        /* OUTPUTS FILE TO <SUBPATH> IN CURRENT DIR */
        sprintf(dirpath,"%s",get_current_dir_name());
        strcat(dirpath,subpath);
        FILE *dbpat = fopen(dirpath,"rb");
        if(dbpat==NULL){printf("Error opening file !");return -1;}
        rewind(dbpat);
        while(feof(dbpat)!=1)
        {
                fread(&pat,sizeof(patient),1,dbpat);
                if(strcmp(pat.nom,nom)==0)
                {fclose(dbpat);return (pat.id) ;}
        }
	fclose(dbpat);
        return -1;
}
int Auth_test(char usr[],char mdp[])
{
	char mdpt[20];
	int essai=0,pwd=0,out=0,test=0,idrech;
	staffmed med1;
        while( (pwd==0) && (essai!=3) )
        {       
                essai++;
                printf("Identifiant :");
                scanf("%s",usr);
                test=0;
                idrech=medtoid("/tmp/dbmed",usr);
                if(idrech!=-1){test=read_med_file("/tmp/dbmed",idrech,&med1);}
                //printf("Mot de passe :");
                strcpy(mdp,getpass("Mot de passe :"));
		strcpy(mdpt,med1.mdp);
                if(strcmp(mdp,mdpt)==0 && (test==1))
                {
                        pwd=1;
                }else printf("Utilisateur ou Mot de passe incorrect ! \n");
        }
        if(pwd==1){out=0;}
        if(essai==3){printf("3 essais depasse !");out=1;}
	return out;
}
